package com.door.erp.dao.hibernate;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.door.erp.dao.ExpenditureDao;
import com.door.erp.dao.base.GenericDaoImpl;
import com.door.erp.model.Employee;
import com.door.erp.model.Expenditure;
import com.door.erp.model.ExpenditureType;

/**
 *类说明
 *
 * @author Xuelin Yang E-mail:yangxuelin@foxmail.com
 * @version 创建时间：2013-6-3 上午9:49:38
 */
@Repository(value="expenditureDao")
public class ExpenditureDaoImpl extends GenericDaoImpl<Expenditure, Long>
		implements ExpenditureDao {

	@Override
	protected Class<Expenditure> getPersistentClass() {
		return Expenditure.class;
	}

	@Override
	public List<Expenditure> getExpenditureByTimeAndType(final Date beginTime, final Date endTime, final Long type, final Long employee) {
		return this.getHibernateTemplate().execute(new HibernateCallback<List<Expenditure>>() {
			@Override
			public List<Expenditure> doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria criteria = session.createCriteria(getPersistentClass());
				if(beginTime != null) {
					criteria.add(Restrictions.ge("createTime", beginTime));
				}
				if(endTime != null) {
					criteria.add(Restrictions.le("createTime", endTime));
				}
				if(type != null) {
					criteria.add(Restrictions.eq("type.id", type));
				}
				if(employee != null) {
					criteria.add(Restrictions.eq("employee.id", employee));
				}
				criteria.addOrder(Order.desc("createTime"));
				return criteria.list();
			}
		});
	}
}
